package com.data.modules.smzh.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.data.common.util.HttpClientUtil;
import com.data.modules.smzh.entity.SmThirdRegist;
import com.data.modules.smzh.mapper.SmThirdRegistMapper;
import com.data.modules.smzh.service.ISmThirdRegistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 时迈三方注册表
 * @Author: data-boot
 * @Date:   2023-05-04
 * @Version: V1.0
 */
@Service
@Slf4j
public class SmThirdRegistServiceImpl extends ServiceImpl<SmThirdRegistMapper, SmThirdRegist> implements ISmThirdRegistService {

    @Autowired
    private SmThirdRegistMapper smThirdRegistMapper;

    @Value("${lqh.registerUrl:}")
    private String thirdUrl;

    @Override
    public void synThirdRegist() {

        //删除临时数据
        smThirdRegistMapper.deleteTempInfo();

        //新增临时数据
        smThirdRegistMapper.batchInsertTempInfo();

        //增量同步 建筑编号 、用户id、用户手机号
        smThirdRegistMapper.addThirdInfo();

        //分组 删除重复建筑编号
        smThirdRegistMapper.deleteRepeatBuildNo();

        //增量同步 建筑编号 、用户id  不相同的
        smThirdRegistMapper.updateUserId();

        //增量同步 建筑编号 、用户id 相同  但是手机号不相同
        smThirdRegistMapper.updateMobieInfo();

        //增量同步 建筑编号  相同  但是手机号不相同 、userId 不相同

        smThirdRegistMapper.updateUserIdAnMobie();

        List<SmThirdRegist> list = this.lambdaQuery().eq(SmThirdRegist::getStatus,1).list();

        list.stream().forEach(x->{

            Map<String,Object> map = new HashMap<>();
            String userName = x.getUserName();
            String passWord = x.getPassWord();
            map.put("username",userName);
            map.put("password",passWord);
            map.put("origin","2001");

            //调用第三方接口
            String result = HttpClientUtil.doPostJson(thirdUrl, JSON.toJSONString(map));

            if(ObjectUtil.isNotEmpty(result)){

                JSONObject jsonObject = JSONObject.parseObject(result);

                String code = jsonObject.getString("code");
                log.info("调用龙侨华注册接口请求参数{},结果{}",JSON.toJSONString(map),code);
                if(ObjectUtil.equals("1",code) || ObjectUtil.equals("7",code)){
                    x.setStatus(2);
                }
                x.setRemark(result);
                this.updateById(x);
            }
        });
    }
}
